const { BrowserWindow, Menu } = require('electron').remote;
const curWin = require('electron').remote.getCurrentWindow();
const ipcRenderer = require('electron').ipcRenderer;
const fs = require('fs');
const path = require('path');
new Vue({
  el: document.getElementById("root"),
  data: {
    projects: [],
    selectedProject: ''
  },
  created() {
    ipcRenderer.on('pushProject', (event, args) => {
      this.projects.push(args.projectName);
    });
  },
  mounted() {
    // fs.opendirSync(path.join("root", "projects")).readSync()
    fs.readdirSync(path.resolve("/data-mock", "projects"), {withFileTypes: true}).forEach(dirent => {
      if (dirent.isDirectory()) {
        this.projects.push(dirent.name);
      }
    });
  },
  methods: {
    createProject() {
      let win = new BrowserWindow({
        width: 400,
        height: 450,
        webPreferences: {
          nodeIntegration: true,
          webSecurity: false
        },
        icon: './favicon.ico',
        autoHideMenuBar: true,
        resizable: false,
        minimizable: false
      });
      win.on('closed', () => win = null );
      win.loadFile('./views/pages/create-project/index.html');
    },
    gotoProjectDetail(project) {
      this.selectedProject = project;
      let win = new BrowserWindow({
        width: 1200,
        height: 800,
        center: true,
        icon: './favicon.ico',
        webPreferences: {
          nodeIntegration: true,
          webviewTag: true
        }
      });
      // win.webContents.openDevTools();

      // 加载index.html文件
      win.loadFile("./views/pages/project-detail/index.html");

      // win.on('closed', () => { win = null });

      // ipcRenderer.send("createWindow_ProjectDetail", win.id);

      win.webContents.on('did-finish-load', () => {
        win.webContents.send('project', project);
        curWin.close();
      });

      Menu.setApplicationMenu(null);
    }
  }
});
